package com.qian.service.ddj.serviceImpl;

import com.qian.api.ddj.model.Config;
import com.qian.api.ddj.service.IConfigService;
import com.qian.service.ddj.dao.ConfigRepository;
import com.qian.service.ddj.entity.DdjConfig;
import com.yaowk.service.common.BaseRepository;
import com.yaowk.service.common.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * created by quziwei on 2018/3/1 0001  09:48
 */
@Service
public class ConfigServiceImpl extends BaseServiceImpl<Config,DdjConfig,Integer> implements IConfigService {
    @Autowired
    private ConfigRepository dao;

    @Override
    protected BaseRepository getDao() {
        return dao;
    }

    @Override
    public Config save(Config model) {
        if (model.isNew()){
            model = simpleSave(model);
        }else{
            DdjConfig config = new DdjConfig();
            config = dao.findOne(model.getId());
            if (isNotEmpty(model.getName())){
                config.setName(model.getName());
            }
            if (isNotEmpty(model.getValue())){
                config.setValue(model.getValue());
            }
            if (isNotEmpty(model.getDescription())){
                config.setDescription(model.getDescription());
            }
            if (isNotEmpty(model.getParentId())){
                config.setParentId(model.getParentId());
            }
            model = entity2model(dao.save(config));
        }
        return model;
    }

    @Override
    protected DdjConfig model2Entity(Config model) {
        DdjConfig entity = null;
        if (isNotEmpty(model)){
            entity = new DdjConfig();
            entity.setDescription(model.getDescription());
            entity.setName(model.getName());
            entity.setValue(model.getValue());
            if (isNotEmpty(model.getParentId())){
                DdjConfig ddjConfig = new DdjConfig();
                ddjConfig.setId(model.getParentId());
               entity.setDdjConfig(ddjConfig);
            }
            entity.setId(model.getId());

        }
        return entity;
    }

    @Override
    protected Config entity2model(DdjConfig entity) {
        Config config = null;
        if (isNotEmpty(entity)){
            config = new Config();
            config.setDescription(entity.getDescription());
            config.setName(entity.getName());
            config.setValue(entity.getValue());
            config.setParentId(entity.getParentId());
            config.setId(entity.getId());
        }
        return config;
    }
}
